home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / Lightwave / Viewer.java.z / Viewer.java
Encoding:
Java Source  |  2003-08-08  |  6.4 KB  |  203 lines

  1. /*
  2.  *    @(#)Viewer.java 1.14 02/10/21 13:44:30
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.*;
  42.  
  43. import javax.media.j3d.BranchGroup;
  44. import javax.media.j3d.Canvas3D;
  45. import javax.media.j3d.Transform3D;
  46. import javax.media.j3d.TransformGroup;
  47. import javax.media.j3d.View;
  48. import javax.vecmath.Matrix4d;
  49.  
  50. import com.sun.j3d.loaders.lw3d.Lw3dLoader;
  51. import com.sun.j3d.loaders.Loader;
  52. import com.sun.j3d.loaders.Scene;
  53. import com.sun.j3d.utils.applet.MainFrame;
  54. import com.sun.j3d.utils.universe.SimpleUniverse;
  55.  
  56.  
  57. /**
  58.  * This class loads in a Lightwave3D file and displays it in an applet
  59.  * window.  The application is fairly basic; a more complete version
  60.  * of a Lightwave 3D loader might incorporate features such as
  61.  * settable clip plane distances and animated views (these are both
  62.  * possible with the current Lightwave 3D loader, they just need to
  63.  * be implemented in the application).
  64.  */
  65. public class Viewer extends Applet {
  66.  
  67.     private java.net.URL filename;
  68.     private SimpleUniverse u;
  69.  
  70.     public Viewer(java.net.URL url) {
  71.     filename = url;
  72.     }
  73.  
  74.     public Viewer() {}
  75.  
  76.     public void init() {
  77.     if (filename == null) {
  78.         // the path to the file for an applet
  79.         try {
  80.         java.net.URL path = getCodeBase();
  81.         filename = new java.net.URL(path.toString() +
  82.                         "./ballcone.lws");
  83.         }
  84.         catch (java.net.MalformedURLException ex) {
  85.         System.err.println(ex.getMessage());
  86.         ex.printStackTrace();
  87.         System.exit(1);
  88.         }
  89.     }
  90.  
  91.     // Construct the Lw3d loader and load the file
  92.     Loader lw3dLoader = new Lw3dLoader(Loader.LOAD_ALL);
  93.     Scene loaderScene = null;
  94.     try {
  95.         loaderScene = lw3dLoader.load(filename);
  96.     }
  97.     catch (Exception e) {
  98.         e.printStackTrace();
  99.             System.exit(1);
  100.     }
  101.  
  102.     // Construct the applet canvas
  103.     setLayout(new BorderLayout());
  104.         GraphicsConfiguration config =
  105.            SimpleUniverse.getPreferredConfiguration();
  106.  
  107.         Canvas3D c = new Canvas3D(config);
  108.     add("Center", c);
  109.  
  110.     // Create a basic universe setup and the root of our scene
  111.     u = new SimpleUniverse(c);
  112.     BranchGroup sceneRoot = new BranchGroup();
  113.  
  114.     // Change the back clip distance; the default is small for
  115.     // some lw3d worlds
  116.     View theView = u.getViewer().getView();
  117.     theView.setBackClipDistance(50000f);
  118.     
  119.     // Now add the scene graph defined in the lw3d file
  120.     if (loaderScene.getSceneGroup() != null) {
  121.         // Instead of using the default view location (which may be
  122.         // completely bogus for the particular file you're loading),
  123.         // let's use the initial view from the file.  We can get
  124.         // this by getting the  view groups from the scene (there's
  125.         // only one for Lightwave 3D), then using the inverse of the
  126.         // transform on that view as the transform for the entire scene.
  127.  
  128.         // First, get the view groups (shouldn't be null unless there
  129.         // was something wrong in the load
  130.         TransformGroup viewGroups[] = loaderScene.getViewGroups();
  131.  
  132.         // Get the Transform3D from the view and invert it
  133.         Transform3D t = new Transform3D();
  134.         viewGroups[0].getTransform(t);
  135.         Matrix4d m = new Matrix4d();
  136.         t.get(m);
  137.         m.invert();
  138.         t.set(m);
  139.  
  140.         // Now we've got the transform we want.  Create an
  141.         // appropriate TransformGroup and parent the scene to it.
  142.         // Then insert the new group into the main BranchGroup.
  143.         TransformGroup sceneTransform = new TransformGroup(t);
  144.         sceneTransform.addChild(loaderScene.getSceneGroup());
  145.         sceneRoot.addChild(sceneTransform);
  146.     }
  147.     
  148.     // Make the scene graph live by inserting the root into the universe
  149.     u.addBranchGraph(sceneRoot);
  150.     }
  151.  
  152.  
  153.     public void destroy() {
  154.     u.cleanup();
  155.     }
  156.  
  157.     /**
  158.      * The main method of the application takes one argument in the
  159.      * args array; the filname that you want to load.  Note that the
  160.      * file must be reachable from the directory in which you're running
  161.      * this application.
  162.      */
  163.     public static void main(String args[]) {
  164.     java.net.URL url = null;
  165.     java.net.URL pathUrl = null;
  166.     if (args.length > 0) {
  167.         try {
  168.         if ((args[0].indexOf("file:") == 0) ||
  169.             (args[0].indexOf("http") == 0)) {
  170.             url = new java.net.URL(args[0]);
  171.         }
  172.         else if (args[0].charAt(0) != '/') {
  173.             url = new java.net.URL("file:./" + args[0]);
  174.         }
  175.         else {
  176.             url = new java.net.URL("file:" + args[0]);
  177.         }
  178.         }
  179.         catch (java.net.MalformedURLException ex) {
  180.         System.err.println(ex.getMessage());
  181.         ex.printStackTrace();
  182.         System.exit(1);
  183.         }
  184.     }
  185.     else {
  186.         // the path to the image for an application
  187.         try {
  188.         url = new java.net.URL("file:./ballcone.lws");
  189.         }
  190.         catch (java.net.MalformedURLException ex) {
  191.         System.err.println(ex.getMessage());
  192.         ex.printStackTrace();
  193.         System.exit(1);
  194.         }
  195.     }
  196.     new MainFrame(new Viewer(url), 500, 500);
  197.     }
  198. }
  199.  
  200.  
  201.  
  202.  
  203.